<?php

/**
 * CheckRangeSchedule function
 *
 * Return schedule for specified date range.
 *
 * @author Will Zhao <kexinzhao666@gmail.com>
 */
include_once 'UTDBGlobal.php';
include_once 'UTerrorcode.php';
include_once 'UTcheckAuth.php';

/**
 * @param $userName string
 * @param $userPassHash string
 * @param $startDate string
 * @param $endDate string
 * @return array|null
 */
function checkRangeSchedule($userName, $userPassHash, $startDate, $endDate) {
   global $dbaddress;
   global $dbuser;
   global $dbpassword;
   global $dbdatabasename;

   if (checkAuthentication($userName, $userPassHash)) {
      $conn = mysqli_connect($dbaddress, $dbuser, $dbpassword, $dbdatabasename);

      //grab all employees
      $sql = "SELECT empID, accName FROM paradigmshift_dev.account";
      $result = mysqli_query($conn, $sql);

      while ($row = $result->fetch_array()) {
         $sql = "select 
                   roles.roleID,
                   roles.roleName,
                   roledate.dateID,
                   dates.datestamp,
                   roledate.startTime,
                   roledate.endTime,
                   roledate.shiftStatus
                   from roledate left join 
                   (dates, roles) on
                   roledate.dateID = dates.dateID and
                   roledate.roleID = roles.roleID 
                   where dates.datestamp between '" . $startDate . "' and '" . $endDate . "' and roledate.empID=" . $row['empID'];
         $shiftsResult = mysqli_query($conn, $sql);
         $shifts = null;
         while ($shiftRow = $shiftsResult->fetch_array()) {
            $shifts[] = array(
                'roleID' => $shiftRow['roleID'],
                'roleName' => $shiftRow['roleName'],
                'dateID' => $shiftRow['dateID'],
                'datestamp' => $shiftRow['datestamp'],
                'startTime' => $shiftRow['startTime'],
                'endTime' => $shiftRow['endTime'],
                'shiftStatus' => $shiftRow['shiftStatus']);
         }
         $employees[] = array(
             'empID' => $row['empID'],
             'accName' => $row['accName'],
             'shifts' => $shifts);
         $error = ErrorCode::successRead;
      }
   } else {
      $error = ErrorCode::authFailRead;
   }

   $output = array(
       'code' => $error,
       'employees' => $employees
   );
   return $output;
}

?>
